
//
// 110112 - AH - Created.
// 110113 - AH - Implemented loading of XML.
//

package org.soekrat.swing.network;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.soekrat.engine.io.Soekrat2Data;
import org.soekrat.engine.io.SoekratXml;
import org.soekrat.engine.network.Network;

/**
 * <p>
 *   Simple matrix based editor for editing a 
 *   {@link Network}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class SimpleNetworkEditor extends JFrame 
{
  private final NetworkTableView m_view=new NetworkTableView();
  
  private JFileChooser m_saveChooser=null;
  
  private JFileChooser m_loadChooser=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  private SimpleNetworkEditor()
  {
    setTitle("Simple Network Editor");
    
    Container c=getContentPane();
    c.setLayout(new BorderLayout());
    
    c.add(m_view, BorderLayout.CENTER);
    
    JPanel north=new JPanel(new FlowLayout(FlowLayout.LEFT));
    north.add
    (
      new JButton
      (
        new AbstractAction("Save")
        {
          public void actionPerformed(ActionEvent e) 
          {
            if (m_saveChooser==null)
            {
              m_saveChooser=new JFileChooser();
              m_saveChooser.setDialogTitle("Save");
            }
            if (m_saveChooser.showSaveDialog(SimpleNetworkEditor.this)==
              JFileChooser.APPROVE_OPTION)
            {
              //try
              {
                Network network=m_view.getNetwork();
                
                // TODO Fix later IF needed...
                //SoekratXml.compile(network, m_saveChooser.getSelectedFile());
              }
              /*
              catch (IOException iox)
              {
                iox.printStackTrace();
              }
              */
            }
          }
        }
      )
    );
    north.add
    (
      new JButton
      (
        new AbstractAction("Load")
        {
          public void actionPerformed(ActionEvent e) 
          {
            if (m_loadChooser==null)
            {
              m_loadChooser=new JFileChooser();
              m_loadChooser.setDialogTitle("Load");
            }
            if (m_loadChooser.showOpenDialog(SimpleNetworkEditor.this)==
              JFileChooser.APPROVE_OPTION)
            {
              try
              {
                Soekrat2Data sd=SoekratXml.parse(
                  m_loadChooser.getSelectedFile());
                Network network=sd.getNetwork();
                if (network!=null)
                {
                  m_view.setNetwork(network);
                }
              }
              catch (IOException iox)
              {
                iox.printStackTrace();
              }
            }
          }
        }
      )
    );
    c.add(north, BorderLayout.NORTH);
    
    pack();
    setSize(500, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  
  public static void main(String[] args) 
  {
    SwingUtilities.invokeLater
    (
      new Runnable() 
      {
        public void run(){ new SimpleNetworkEditor().setVisible(true); }
      }
    );  
  }
  
}
